import unittest

import requests

from api.api_register_login import ApiRegisterLogin
from api.api_trust_recharge import ApiTrustRecharge
from util import parser_html, read_json
from parameterized import parameterized

class TestTrustRecharge(unittest.TestCase):
    def setUp(self) -> None:
        # 获取session
        self.session = requests.session()
        # 获取ApiTrustRecharge实例
        self.trust = ApiTrustRecharge(self.session)
        # 登录成功
        ApiRegisterLogin(self.session).api_login()

    def tearDown(self) -> None:
        self.session.close()

    # 1、认证接口 测试
    @parameterized.expand(read_json("trust_recharge.json","approve"))
    def test01_approve(self, realname, card_id, expect_text):
        try:
            # 调用认证接口
            result = self.trust.api_approve(realname, card_id)
            # 断言
            self.assertIn(expect_text, result.text)
        except Exception as e:
            # 日志
            print('错误原因：', e)
            # 抛异常
            raise

    # 2、获取认证接口 测试
    @parameterized.expand(read_json("trust_recharge.json","approve_info"))
    def test02_get_approve(self, expect_text):
        try:
            # 调用获取认证接口
            result = self.trust.api_get_approve()
            self.assertIn(expect_text, result.text)
        except Exception as e:
            # 日志
            print('错误原因：', e)
            # 抛异常
            raise

            # 3、请求后台开户接口 测试

    @parameterized.expand(read_json("trust_recharge.json","trust"))
    def test03_trust(self, expect_text):
        try:
            # 调用后台开户接口
            result = self.trust.api_trust()
            # 调用三方开户
            r = parser_html(result)
            result = self.session.post(url=r[0], data=r[1])
            print("三方开户的结果为：", result.text)
            self.assertIn(expect_text, result.text)
        except Exception as e:
            # 日志
            print('错误原因：', e)
            # 抛异常
            raise

    # 4、获取充值验证码接口 测试
    @parameterized.expand(read_json("trust_recharge.json","img_code"))
    def test04_img_code(self, random, expect_code):
        try:
            # 调用充值验证码
            r = self.trust.api_img_code(random)
            self.assertEqual(expect_code, r.status_code)
        except Exception as e:
            # 日志
            print('错误原因：', e)
            # 抛异常
            raise

    # 5、请求后台充值接口 测试
    @parameterized.expand(read_json("trust_recharge.json","recharge"))
    def test05_recharge(self, valicode, expect_text):
        try:
            # 1、调用充值验证码
            self.trust.api_img_code(1231223)
            # 2、调用请求后台充值接口
            result = self.trust.api_recharge(valicode)
            if valicode != 8888:
                self.assertIn(expect_text,result.text)
            else:
                # 3、调用三方充值
                r = parser_html(result)
                result = self.session.post(url=r[0], data=r[1])
                print("三方充值的结果为：", result.text)
                self.assertIn(expect_text, result.text)
        except Exception as e:
            # 日志
            print('错误原因：', e)
            # 抛异常
            raise
